#include <iostream>
#include <string>
#include <vector>

#include "friend.pb.h"
#include "mprpcapplication.h"
#include "rpcprovider.h"
#include "logger.h"

class FriendService : public fixbug::FriendServiceRpc
{
public:
    // 本地业务，获取好友列表
    std::vector<std::string> GetFriendsList(uint32_t userid)
    {
        std::cout << "do GetFriendsList service! userid:" << userid << std::endl;
        std::vector<std::string> vec;
        vec.push_back("Su");
        vec.push_back("Jing");
        vec.push_back("Yan");
        return vec;
    }

    // 重写基类方法
    void GetFriendList(::google::protobuf::RpcController* controller,
                       const ::fixbug::GetFriendListRequest* request,
                       ::fixbug::GetFriendListResponse* response,
                       ::google::protobuf::Closure* done)
    {
        uint32_t userid = request->userid();

        // 执行本地业务
        std::vector<std::string> friendsList = GetFriendsList(userid);

        // 填写响应数据
        response->mutable_result()->set_errcode(0);
        response->mutable_result()->set_errmsg("");
        for(std::string &str: friendsList)
        {
            std::string *p = response->add_friends();
            *p = str;
        }

        done->Run();
    }
};

int main(int argc, char **argv)
{
    LOG_INFO("FIRST LOG MESG");
    LOG_ERROR("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__);

    // 调用框架的初始化操作 provider -i config.conf
    MprpcApplication::Init(argc, argv);

    // provider 是一个 rpc 网络服务对象，把 FriendService 对象发布到 rpc 节点上
    RpcProvider provider;
    provider.NotifyService(new FriendService);

    // 启动一个 rpc 发布节点，Run以后，进程进入阻塞状态，等待远程的 rpc 调用请求
    provider.Run();

    return 0;
}